package com.example.myapplication.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.myapplication.database.room.database.UserDatabase;
import com.example.myapplication.database.room.pojo.Users;

public class MyRoomProvider extends ContentProvider {

    private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    /**
     * 添加规则
     */
    static {
        uriMatcher.addURI("com.example.myapplication.provider.MyRoomProvider",null,1);
    }

    private UserDatabase userDatabase;

    @Override
    public boolean onCreate() {
        userDatabase = UserDatabase.getInstance(this.getContext());
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        int match = uriMatcher.match(uri);
        if(match == 1){
            return userDatabase.userDao().getAll1();
        }else {
            throw new IllegalArgumentException("参数错误！");
        }
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        int match = uriMatcher.match(uri);
        if(match == 1){
            Users user = new Users();
            if (values != null){
                user.setUserName(values.get("name").toString());
                user.setUserAge((Integer) values.get("age"));
                user.setUserHeight((Integer) values.get("height"));
            }
            userDatabase.userDao().insert(user);
            // 通知监听者数据发生变化
            getContext().getContentResolver().notifyChange(uri,null);
            return uri;
        }else {
            throw new IllegalArgumentException("参数错误！");
        }
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        int match = uriMatcher.match(uri);
        if(match == 1){
            Users user = new Users();
            userDatabase.userDao().delete(user);
            // 通知监听者数据发生变化
            getContext().getContentResolver().notifyChange(uri,null);
            return 1;
        }else {
            throw new IllegalArgumentException("参数错误！");
        }
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        int match = uriMatcher.match(uri);
        if(match == 1){
            Users user = new Users();
            if (values != null){
                user.setUserAge((Integer) values.get("age"));
                user.setUserName(values.get("name").toString());
            }
            userDatabase.userDao().update(user);
            // 通知监听者数据发生变化
            getContext().getContentResolver().notifyChange(uri,null);
            return 1;
        }else {
            throw new IllegalArgumentException("参数错误！");
        }
    }
}
